package vlog

import (
	"orm_gin/model"
	"orm_gin/orm"
	"orm_gin/utools/res"
	"time"

	"github.com/gin-gonic/gin"
)

// @BasePath /vlog

// @Summary 新增
// @Description token
// @Tags Vlog
// @Security ApiKeyAuth
// @Param Title query string true "Title"
// @Param Content query string true "Content"
// @Success 200 {object} docsmodel.Response
// @Router /vlog [post]
// @Security
func AddVlog(c *gin.Context) {
	// 是否有vlog 表，没有新增
	if has := orm.DB.Migrator().HasTable(&model.UserVlog{}); !has {
		err := orm.DB.AutoMigrate(&model.UserVlog{})
		if err != nil {
			res.Error(c, 0, err, "创建表失败")
			return
		}
	}

	// 插入数据
	var data model.UserVlog
	id, _ := c.Get("UserId")
	err := c.BindJSON(&data)
	if err != nil {
		res.Error(c, 0, err, "解析params JSON 失败")
		return
	}

	data.CreatedAt = time.Now()
	data.UpdatedAt = time.Now()
	data.UserInfoID = id.(string)
	r := orm.DB.Create(&data)

	if r.Error != nil {
		res.Error(c, 0, r.Error, "插入数据库时错误")
		return
	}

	res.Ok(c, data, "操作成功")
}
